import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

enum _ChannelType {
  methodChannel,
  basicMessageChannel,
  eventChannel,
}

class ChannelDemoPage extends StatefulWidget {
  const ChannelDemoPage({Key? key}) : super(key: key);

  @override
  _ChannelDemoPageState createState() => _ChannelDemoPageState();
}

class _ChannelDemoPageState extends State<ChannelDemoPage> {
  _ChannelType _currentChannel = _ChannelType.methodChannel;

  String Method_Channel = "com.example.terminal_flutter_demo/methodChannel";
  Future<void> testMethodCall() async {
    //1.创建Flutter端的MethodChannel
    MethodChannel _methodChannel = MethodChannel(Method_Channel);
    //2.通过invokeMethod调用Native方法，拿到返回值
    String debugString = await _methodChannel.invokeMethod(
        "testMethodCall", "give me debug info");
    print('test debugString=$debugString');
  }

  Widget _getContent() {
    switch (_currentChannel) {
      case _ChannelType.methodChannel:
        return Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ElevatedButton(onPressed: () {}, child: const Text('调用原生方法'))
          ],
        );
      case _ChannelType.basicMessageChannel:
        return Text('_currentChannel');
      case _ChannelType.eventChannel:
        return Text('_currentChannel');
      default:
        return const Text('');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            ElevatedButton(
                onPressed: () {
                  setState(() {
                    _currentChannel = _ChannelType.methodChannel;
                  });
                },
                child: const Text('MethodChannel')),
            ElevatedButton(
                onPressed: () {
                  setState(() {
                    _currentChannel = _ChannelType.basicMessageChannel;
                  });
                },
                child: const Text('BasicMessageChannel')),
            ElevatedButton(
                onPressed: () {
                  setState(() {
                    _currentChannel = _ChannelType.eventChannel;
                  });
                },
                child: const Text('EventChannel')),
          ],
        ),
        Expanded(
          flex: 1,
          child: Center(
            child: _getContent(),
          ),
        ),
      ],
    );
  }
}
